/*
Content: Alternative specification with rolling sums and lags
Replicates: Table 5
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/

	
*********************************************
***** MERGE SOEP TO DAILY ATTACKS ************
*********************************************
use "${data}/rawdata_soep.dta", clear
drop if pid==.
drop if corigin==.
rename pmonin imonth

* Interview date 			
foreach var in hlk0059 ptagin hlk0059 {
replace `var' = 15 if `var' == .
}

gen startdate = mdy(imonth,hlk0059,syear)
gen startdate2 =  mdy(imonth,ptagin,syear)
gen startdate3 =  mdy(hlk0060,hlk0059,syear)
gen startdate4 = mdy(pgmonth,hlk0059,syear)
gen startdate5 =  mdy(pgmonth,ptagin,syear)


replace startdate = startdate2 if startdate == .
replace startdate = startdate3 if startdate == .
replace startdate = startdate4 if startdate == .
replace startdate = startdate5 if startdate == .
	
g idate =ym(syear, imonth)
drop if startdate==.

* Country of origin

cap drop soep_corigin_id
clonevar soep_corigin_id = corigin


	* Fix the soep_corigin_id from the SOEP the vlaues for kosovo and albania and eastern europe
		* 128 Malaysia 
		replace soep_corigin_id = 104 if soep_corigin_id == 128
		* 155 Turkmenistan
		replace soep_corigin_id = 91 if soep_corigin_id == 155
		* 7 Germany
		replace soep_corigin_id = 1 if soep_corigin_id == 7
		* USA
		replace soep_corigin_id = 18 if soep_corigin_id == 164
		 *168 Montenegro  
		replace soep_corigin_id = 106 if soep_corigin_id == 168
		*145 Mongolia  129 Samoa missing
	
* Reduce dataset to key variables
drop pg* plb* hlk* plj* plh* bire* bii* pla* pab* plc* pld* plf* ple* plg* pli* plm* p_k* p_n* p_buh* hlc* hlf* hlf* hli* hlj* hac* hld* hlh*  hle* pag* pka* plk* p_is* p_a* living* f*8 m*8 f*92 m*92 pdaue* pinta* p_f* p_d* ic*

* Matching date - Month-Year of interview with Month-Year of Event
g year=year(startdate)
g month=month(startdate)
g startmy=ym(year, month)
format startmy %tm

drop if syear==. // ATENTION DROP WHEN NO SURVEY DATA
drop  year month 


fmerge m:1 soep_corigin_id startdate  using "$globalterror\terror_dailydata_rolling_final.dta", gen(eventmerge_m)
drop if eventmerge_m==2

cap drop year month startmy

g year=year(startdate)
g month=month(startdate)
g startmy=ym(year, month)
format startmy %tm
					

save "$data/dailyfinaldata_terrorsoep_rolling_final.dta", replace


********************************************************************************
** DAILY ATTACKS REGRESSIONS
********************************************************************************

use "$data/dailyfinaldata_terrorsoep_rolling_final.dta", clear

* ATTENTON: match sample time period to IEB
drop if syear>2018
drop if syear<2000
drop if soep_corigin_id==1 // drop Germans



********************************************************************************
*** Table 4
********************************************************************************
	
* EXPOSURE FOR DIFFERENT DISTANCES TO INTERVIEW 

est clear 
foreach out in remain_ger_per {
	
	foreach day in 90 {	
		
global select_reg_M "i.soep_corigin_id#i.syear i.bula i.syear#i.month"  
global controls "i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child " //**/
	
cap drop attack_d
clonevar attack_d=atleast1terror_15d
qui eststo m1:  reghdfe `out' attack_d $controls c.ln_cum_terror_365l_15s, vce(cluster soep_corigin_id) absorb($select_reg_M)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
qui estadd local lagged "Yes"
sum	 remain_ger_per if e(sample)==1 
estadd scalar ymean = r(mean)

cap drop attack_ln
clonevar attack_ln=ln_cum_terror_15d
qui eststo m2:  reghdfe `out' attack_ln $controls c.ln_cum_terror_365l_15s, vce(cluster soep_corigin_id) absorb($select_reg_M)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
qui estadd local lagged "Yes"
sum	 remain_ger_per if e(sample)==1 
estadd scalar ymean = r(mean)

cap drop attack_d
clonevar attack_d=atleast1terror_30d
qui eststo m3:  reghdfe `out' attack_d $controls c.ln_cum_terror_365l_30s, vce(cluster soep_corigin_id) absorb($select_reg_M)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
qui estadd local lagged "Yes"
sum	 remain_ger_per if e(sample)==1 
estadd scalar ymean = r(mean)

cap drop attack_ln
clonevar attack_ln=ln_cum_terror_30d
qui eststo m4:  reghdfe `out' attack_ln $controls c.ln_cum_terror_365l_30s, vce(cluster soep_corigin_id) absorb($select_reg_M)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
qui estadd local lagged "Yes"
sum	 remain_ger_per if e(sample)==1 
estadd scalar ymean = r(mean)

cap drop attack_d
clonevar attack_d=atleast1terror_90d
qui eststo m5: reghdfe `out' attack_d $controls c.ln_cum_terror_365l_90s, vce(cluster soep_corigin_id) absorb($select_reg_M)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
qui estadd local lagged "Yes"
sum	 remain_ger_per if e(sample)==1 
estadd scalar ymean = r(mean)

cap drop attack_ln
clonevar attack_ln=ln_cum_terror_90d
qui eststo m6: reghdfe `out' attack_ln $controls c.ln_cum_terror_365l_90s, vce(cluster soep_corigin_id) absorb($select_reg_M)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
qui estadd local lagged "Yes"
sum	 remain_ger_per if e(sample)==1 
estadd scalar ymean = r(mean)
	}
	
}

esttab m* using "$table/table4_final.tex",  star(* 0.10 ** 0.05 *** 0.01) se(3) b(4)   keep(attack_d attack_ln) nocons nobase label nonotes nogaps noobs ///
    mgroups("Intentions to remain permanently in Germany",   pattern(1 0 0 0 0 0)    prefix(\multicolumn{@span}{c}{) suffix(})  span erepeat(\cmidrule(lr){@span})) ///
    mtitle("15 days" "" "1 month" "" "3 months" "")  posthead("\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7}"   "& (1) & (2) & (3) & (4) & (5) & (6) \\") ///
    varlabel(attack_d "Terrorist attacks \$>\$ 0"    attack_ln "Terrorist attacks (log)") ///
    scalars("nobs Observations" ///
            "ymean Mean intention to stay" ///
            "orig_y Origin country x Year FE" ///
            "date Month FE x Year FE" ///
            "orig_m Origin country x Month FE" ///
            "state State of Residency FE" ///
            "indiv Indiv. Controls" ///
            "lagged Lagged terrorist attacks") ///
    sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s %1s)  replace
		